/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package taskgraph.channels;

import java.io.IOException;

import taskgraph.Config;
import taskgraph.pipes.LongInputPipe;
import taskgraph.pipes.LongOutputPipe;
import taskgraph.ports.LongInputPort;
import taskgraph.ports.LongOutputPort;


/**
 * A channel that transports {@code long} values.
 * <p>Longs are stored into, and read from, a FIFO buffer in their
 * primitive type representation without being byte-streamed.  
 * 
 * @author Armando Blancas
 */
public class LongChannel {
	
	private static final int DEFAULT_BUFFER_SIZE = Config.get().channelCapacity();
	
	private final LongOutputPipe output;
    private final LongInputPipe input;
	private final LongOutputPort outputPort;
    private final LongInputPort inputPort;
    
    /**
     * Creates a channel with the default buffer size.
     */
    public LongChannel() {
    	this(DEFAULT_BUFFER_SIZE);
    }
    
    /**
     * Creates a channel with the passed buffer size.
     * 
     * @param bufferSize The buffer capacity in bytes. 
     */
    public LongChannel(final int bufferSize) {
        if (bufferSize <= 0) {
            throw new IllegalArgumentException("LongChannel bufferSize <= 0");
         }
        
		try {
			output = new LongOutputPipe();
			input = new LongInputPipe(output, bufferSize);
			outputPort = new OutputPort();
			inputPort = new InputPort();
		} catch (IOException e) {
			throw new Error(e);
		}
    }
    
    /**
     * Gets a reference to the output port.
     * 
     * @return An output interface to this channel.
     */
    public LongOutputPort getOutputPort() {
    	return outputPort;
    }
    
    /**
     * Gets a reference to the input port.
     * 
     * @return An input interface to this channel.
     */
    public LongInputPort getInputPort() {
    	return inputPort;
    }

    /**
     * A private implementation of the long output port. It has direct access
     * to this channel and the long input port. Instances of this class are 
     * published through the LongOutputPort interface. 
     */
    private class OutputPort implements LongOutputPort {
    
		@Override
		public LongOutputPort write(long l) throws IOException {
			output.write(l);
			return this;
		}
	
		@Override
		public LongOutputPort write(long[] l) throws IOException {
			output.write(l);
			return this;
		}
	
		@Override
		public LongOutputPort write(long[] l, int off, int len) 
		throws IOException {
			output.write(l, off, len);
			return this;
		}
	
		@Override
		public void close() {
			output.close();
		}
	
    }
    
    /**
     * A private implementation of the long input port. It has direct access
     * to this channel and the long output port. Instances of this class are 
     * published through the LongInputPort interface. 
     */
    private class InputPort implements LongInputPort {

		@Override
		public long read() throws IOException {
			return input.read();
		}
	
		@Override
		public int read(long[] l) throws IOException {
			return input.read(l);
		}
	
		@Override
		public int read(long[] l, int off, int len) throws IOException {
			return input.read(l, off, len);
		}
	
		@Override
	    public final int skipLongs(int n) throws IOException {
			return input.skipLongs(n);
		}
	
		@Override
		public void close() {
			input.close();
		}
	
    }

}
